import { HOME_ITEM_CONSTRUCT, HOME_ITEM_LIST } from '../constants/home'

const INITIAL_STATE = {
  data: {
    configuration: [
      {
        type: '',
        content: {
          name: '',
          mediaContent: []
        }
      }
    ]
  },
  items: [],
};

export default function home (state = INITIAL_STATE, action) {
  switch (action.type) {
    case HOME_ITEM_CONSTRUCT:
      const { data } = action.payload;
      const configuration = (JSON.parse(data.configuration || [])).map(item=>JSON.parse(item.content));
      return {
        ...state,
        data: {
          configuration
        }
      };
    case HOME_ITEM_LIST:
      const { items } = action.payload;
      return {
        ...state,
        items: items
      };
    default:
      return state
  }
}
